package org.visionlibrary.image.acquisition;

import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.SampleModel;
import java.io.File;

import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.TiledImage;

public abstract class AbstractImageFactory {
	public TiledImage load(File file) {
		if (file.exists() && file.canRead()) {
			PlanarImage image = JAI.create("fileload", file.getAbsolutePath());
			TiledImage src = new TiledImage(image, true);

			return src;
		}

		return null;
	}

	public TiledImage load(String filepath) {
		if (filepath != null && filepath.trim().length() > 0) {
			File file = new File(filepath);
			return this.load(file);
		}

		return null;
	}

	public void store(TiledImage image, String filename, String format) {
		if (image != null) {
			if (filename != null && filename.trim().length() > 0) {
				if (format != null && format.trim().length() > 0) {
					JAI.create("filestore", image, filename, format);
				}
			}
		}
	}

	public TiledImage createCompatibleImage(TiledImage src) {
		return this.createCompatibleImage(src, src.getWidth(), src.getHeight());
	}

	public TiledImage createCompatibleImage(TiledImage src, int width,
			int height) {
		TiledImage newTI = new TiledImage(0, 0, width, height, src
				.getTileGridXOffset(), src.getTileGridYOffset(), src
				.getSampleModel(), src.getColorModel());

		return newTI;
	}

	public TiledImage createCompatibleGrayScaleImage(TiledImage src) {
		return createCompatibleGrayScaleImage(src, src.getWidth(), src
				.getHeight());
	}
	
	public TiledImage createCompatibleGrayScaleImage(TiledImage src, int width,
			int height) {
		ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
		int bits[] = new int[] { 8 };
		ColorModel cm = new ComponentColorModel(cs, bits, false, false,
				Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

		SampleModel oldCsm = src.getSampleModel();
		SampleModel csm = new ComponentSampleModel(DataBuffer.TYPE_BYTE, oldCsm
				.getWidth(), oldCsm.getHeight(), 1, oldCsm.getWidth(),
				new int[] { 0 });

		TiledImage newTI = new TiledImage(0, 0, width, height, src
				.getTileGridXOffset(), src.getTileGridYOffset(), csm, cm);

		return newTI;
	}

	public TiledImage createCompatible8BitImage(TiledImage src, int width,
			int height) {
		ColorSpace cs = src.getColorModel().getColorSpace();
		int bits[] = new int[] { 8 };
		ColorModel cm = new ComponentColorModel(cs, bits, false, false,
				Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

		SampleModel oldCsm = src.getSampleModel();
		SampleModel csm = new ComponentSampleModel(DataBuffer.TYPE_BYTE, oldCsm
				.getWidth(), oldCsm.getHeight(), 1, oldCsm.getWidth(),
				new int[] { 0 });

		TiledImage newTI = new TiledImage(0, 0, width, height, src
				.getTileGridXOffset(), src.getTileGridYOffset(), csm, cm);

		return newTI;
	}

	public TiledImage createCompatible8BitImage(TiledImage src) {
		return createCompatible8BitImage(src, src.getWidth(), src.getHeight());
	}
	//TODO do poprawki (glupie zalozenie)
	public int[] getDefaultBackGroundColor(TiledImage src) {
		int bands = src.getNumBands();
		int[] result = new int[bands];
		java.util.Arrays.fill(result, 0);
		return result;
	}
}
